package com.youlai.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.common.util.DateUtils;
import com.youlai.system.converter.AirPlaneConverter;
import com.youlai.system.mapper.AirPlaneMapper;
import com.youlai.system.model.bo.AirPlaneBo;
import com.youlai.system.model.entity.AirPlane;
import com.youlai.system.model.form.AirPlaneForm;
import com.youlai.system.model.query.AirPlaneQuery;
import com.youlai.system.model.vo.AirPlaneVO;
import com.youlai.system.service.AirPlaneService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 机型管理服务实现类
 *
 * @author youlai买国涛
 * @since 2024-11-05 16:30
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class AirPlaneServiceImpl extends ServiceImpl<AirPlaneMapper, AirPlane> implements AirPlaneService {

    private final AirPlaneConverter airPlaneConverter;
    private final AirPlaneMapper airPlaneMapper;

    /**
     * 获取机型管理分页列表
     *
     * @param queryParams 查询参数
     * @return {@link IPage<AirPlaneVO>} 机型管理分页列表
     */
    @Override
    public IPage<AirPlaneVO> getAirPlanePage(AirPlaneQuery queryParams) {
        Page<AirPlaneVO> pageVO = this.baseMapper.getAirPlanePage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }

    /**
     * 获取机型管理表单数据
     *
     * @param id 机型管理ID
     * @return
     */
    @Override
    public AirPlaneForm getAirPlaneFormData(Long id) {
        AirPlane entity = this.getById(id);
        return airPlaneConverter.toForm(entity);
    }

    /**
     * 新增机型管理
     *
     * @param formData 机型管理表单对象
     * @return
     */
    @Override
    public boolean saveAirPlane(AirPlaneForm formData) {
        AirPlane entity = airPlaneConverter.toEntity(formData);
        return this.save(entity);
    }

    /**
     * 更新机型管理
     *
     * @param id   机型管理ID
     * @param formData 机型管理表单对象
     * @return
     */
    @Override
    public boolean updateAirPlane(Long id,AirPlaneForm formData) {
        AirPlane entity = airPlaneConverter.toEntity(formData);
        return this.updateById(entity);
    }

    /**
     * 删除机型管理
     *
     * @param ids 机型管理ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteAirPlanes(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的机型管理数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    public IPage<AirPlaneVO> getPlaneNotFly(AirPlaneQuery queryParams) {
        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<AirPlaneBo> page = new Page<>(pageNum, pageSize);

        // 格式化为数据库日期格式，避免日期比较使用格式化函数导致索引失效
        DateUtils.toDatabaseFormat(queryParams, "startTime", "endTime");


        // 查询数据
        Page<AirPlaneBo> planePage = this.baseMapper.getPlaneNotFly(page, queryParams);
        log.info("开始将bo对象转换为vo对象");
        Page<AirPlaneVO> vo = airPlaneConverter.toAirPlanePageVo(planePage);
        log.info("转换完成，共计{}条数据", vo.getTotal());


        System.out.println(vo.toString());  // 打印分页VO数据

        // 如果需要打印每个元素的内容
        vo.getRecords().forEach(airPlaneVO -> {
            System.out.println(airPlaneVO.toString()); // 打印每个AirPlaneVO对象
        });


        return vo;
    }

    @Override
    public AirPlane getPlaneById(long planeId) {
        return airPlaneMapper.getPlaneById(planeId);
    }

}
